/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.procurement.web;

import java.util.List;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.javaeye.common.web.PageInfo;
import com.javaeye.procurement.dto.Supplier;
import com.javaeye.store.dto.MaterialsCategory;
import com.javaeye.procurement.service.ISupplierService;
import com.javaeye.store.service.IMaterialsCategoryService;

public class SupplierAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1578291709804889611L;

	private static Log log = LogFactory.getLog(SupplierAction.class); 

	/**
	 * 用于生成查询画面上选择分类的下拉框的值
	 */
	private List<MaterialsCategory> rootCategoryList;

	/**
	 * 用于显示一览结果集
	 */
	private List<Supplier> supplierList;
	
	/**
	 * 用于接收分页值对象
	 */
	private PageInfo pageInfo;
	
	/**
	 * 用于接收查询条件值对象
	 */
	private Supplier condition;
	
	/**
	 * 用于接收添加画面和编辑画面的值对象
	 */
	private Supplier supplier;
	
	/**
	 * 用于接收需要修改和删除的ID值
	 */
	private String supplierId;
	
	/**
	 * 应用autocomplete的查询使用接口
	 */
	private String q;
	
	/**
	 * Service
	 */
	private ISupplierService service;
	
	private IMaterialsCategoryService categoryService;
	
	
	public SupplierAction() {
		pageInfo = new PageInfo();
		pageInfo.setPageIndex(0);
		condition = new Supplier();
		condition.setName("");
		condition.setArea("");
		condition.getCategory().setId(-1);
	}
	
	public void setService(ISupplierService service) {
		this.service = service;
	}
	
	public void setCategoryService(IMaterialsCategoryService categoryService) {
		this.categoryService = categoryService;
	}
	
	public String queryAutoCompleteNames() {
		supplierList = service.getAutoCompleteSuppliers(q, 10);
		return SUCCESS;
	}
	
	/**
	 * 条件分页查询
	 * @return
	 * @throws Exception
	 */
	public String querySupplierList() throws Exception {
		log.debug("currentPage=" + pageInfo.getPageIndex() + 
				", area=" + condition.getArea() + 
				", name=" + condition.getName());
		rootCategoryList = categoryService.getCategorys(0);
		supplierList = service.getSupplierList(condition, pageInfo);
		return SUCCESS;
	}
	
	/**
	 * 保存
	 * @return
	 * @throws Exception
	 */
	public String saveSupplier() throws Exception {
		service.saveSupplier(supplier);
		return SUCCESS;
	}
	
	/**
	 * 查询用于添加
	 * @return
	 * @throws Exception
	 */
	public String queryForAdd() throws Exception {
		rootCategoryList = categoryService.getCategorys(0);
		return SUCCESS;
	}
	
	/**
	 * 查询用于编辑
	 * @return
	 * @throws Exception
	 */
	public String querySupplier() throws Exception {
		rootCategoryList = categoryService.getCategorys(0);
		supplier = service.getSupplier(supplierId);
		return SUCCESS;
	}
	
	/**
	 * 删除
	 * @return
	 * @throws Exception
	 */
	public String deleteSupplier() throws Exception {
		boolean result = service.removeSupplier(supplierId);
		querySupplierList();
		if (!result) {
			List<Object> params = new ArrayList<Object>();
			params.add("供应商");
			this.addActionError(getText("actionErrorDeleteFK", params));
			return INPUT;
		}
		return SUCCESS;
	}

	public List<Supplier> getSupplierList() {
		return supplierList;
	}

	public void setSupplierList(List<Supplier> supplierList) {
		this.supplierList = supplierList;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	public Supplier getCondition() {
		return condition;
	}

	public void setCondition(Supplier condition) {
		this.condition = condition;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	public String getSupplierId() {
		return supplierId;
	}

	public void setSupplierId(String supplierId) {
		this.supplierId = supplierId;
	}

	public String getQ() {
		return q;
	}

	public void setQ(String q) {
		this.q = q;
	}
	
	public List<MaterialsCategory> getRootCategoryList() {
		return rootCategoryList;
	}
	
	public void setRootCategoryList(List<MaterialsCategory> rootCategoryList) {
		this.rootCategoryList = rootCategoryList;
	}
}
